package com.kymatrix.mpo.plus.bpm.server.controller.definition;

import com.kymatrix.mpo.plus.bpm.server.model.entity.definition.BpmFormEntity;
import com.kymatrix.mpo.plus.bpm.server.model.form.form.BpmFormResp;
import com.kymatrix.mpo.plus.bpm.server.model.form.form.PageQueryBpmFormReq;
import com.kymatrix.mpo.plus.bpm.server.model.form.form.PageQueryBpmFormResp;
import com.kymatrix.mpo.plus.bpm.server.model.form.form.SaveBpmFormReq;
import com.kymatrix.mpo.plus.bpm.server.model.form.form.UpdateBpmFormReq;
import com.kymatrix.mpo.plus.bpm.server.service.BpmFormService;
import com.kymatrix.mpo.plus.common.business.common.PageResp;
import com.kymatrix.mpo.plus.common.business.common.ResultResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.annotation.Resource;
import javax.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static com.kymatrix.mpo.plus.common.util.CollectionUtils.convertList;

@Api(tags = "BPM 动态表单相关请求处理类")
@RestController
@Validated
public class BpmFormController {

    @Resource
    private BpmFormService formService;

    @PostMapping("/bpm/form/create")
    @ApiOperation(value = "创建动态表单")
    public ResultResp<String> createForm(@Valid @RequestBody SaveBpmFormReq createReqVO) {
        return ResultResp.ok(formService.createForm(createReqVO));
    }

    @PutMapping("/bpm/form/update")
    @ApiOperation(value = "更新动态表单")
    public ResultResp<Boolean> updateForm(@Valid @RequestBody UpdateBpmFormReq updateReqVO) {
        formService.updateForm(updateReqVO);
        return ResultResp.ok(true);
    }

    @DeleteMapping("/bpm/form/delete")
    @ApiOperation(value = "删除动态表单")
    public ResultResp<Boolean> deleteForm(@RequestParam("id") String id) {
        formService.deleteForm(id);
        return ResultResp.ok(true);
    }

    @GetMapping("/bpm/form/get")
    @ApiOperation(value = "获得动态表单")
    public ResultResp<BpmFormResp> getForm(@RequestParam("id") String id) {
        BpmFormEntity entity = formService.getForm(id);
        return ResultResp.ok(BpmFormResp.fromEntity(entity));
    }

    @GetMapping({"/bpm/form/list-all-simple"})
    @ApiOperation(value = "获得动态表单的精简列表", notes = "用于表单下拉框")
    public ResultResp<List<BpmFormResp>> getFormSimpleList() {
        List<BpmFormEntity> list = formService.getFormList();
        return ResultResp.ok(convertList(list, BpmFormResp::fromEntity));
    }   

    @GetMapping("/bpm/form/page")
    @ApiOperation(value = "获得动态表单分页")
    public ResultResp<PageResp<PageQueryBpmFormResp>> getFormPage(@Valid PageQueryBpmFormReq pageVO) {
        PageResp<BpmFormEntity> pageResult = formService.getFormPage(pageVO);
        PageResp<PageQueryBpmFormResp> resp = new PageResp<>();
        resp.setTotal(pageResult.getTotal());
        for (BpmFormEntity bpmFormEntity : pageResult.getList()) {
            resp.getList().add(PageQueryBpmFormResp.fromEntity(bpmFormEntity));
        }
        return ResultResp.ok(resp);
    }

}
